﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using DAL_V1.App_Code.DBBroker;
using EntityObjects;


namespace PresentationLayer.View.StoreClerk
{
    public partial class MakeStationeryAdjustment1 : System.Web.UI.Page
    {

                List<Category> categ_list = new List<Category>();
                List<Catalogue> catal_list = new List<Catalogue>();
        protected void Page_Load(object sender, EventArgs e)
        {
                btnSubmit.Visible = false;
                DateTime today = DateTime.Now;
                //string adjDate = today.ToString("yyyy-MM-dd");
                string adjDate = today.ToString("MM/dd/yyyy");
                lblTodayDate.Text = adjDate;
                
               if (!IsPostBack)
                {
                    categ_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectAllCategory();
                    ddlCategory.DataSource = categ_list;
                    ddlCategory.DataTextField = "CategoryName";
                    ddlCategory.DataValueField = "CategoryId";
                    ddlCategory.DataBind();

                  // for All Catalogue  catal_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectAllCatalogue();
                    //if (catal_list == null)
                    //{
                    //    gvCatalist.DataSource = null;
                    //    gvCatalist.DataBind();
                    //}
                    //else
                    //{
                    //    gvCatalist.DataSource = catal_list;
                    //    gvCatalist.DataBind();
                    //}
                }


        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
                int sel = Convert.ToInt32(ddlCategory.SelectedValue);
                catal_list = MakeStationeryAdjController.getMakeStationeryAdjController().selectCatalogueByCategoryId(sel);
            if (catal_list == null)
            {
                gvCatalist.DataSource = null;
                gvCatalist.DataBind();
            }
            else
            {
                gvCatalist.DataSource = catal_list;
                gvCatalist.DataBind();
            }
                ViewState["Vcatalist"] = catal_list;
                ViewState["selCat"] = ddlCategory.SelectedItem.Text;
                btnConfirm.Visible = true;
     
          }

        protected void gvCatalist_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
                gvCatalist.PageIndex = e.NewPageIndex;
                gvCatalist.DataSource = ViewState["Vcatalist"];
                gvCatalist.DataBind();
           
        }

       
        protected void btnConfirm_Click(object sender, EventArgs e)
        {
                //To find obj in Dropdownlist Catlist
                List<Catalogue> Catlist = new List<Catalogue>();
                //for GridView Obj list
                List<AdjustmentCatalogue> AdjCatlist = new List<AdjustmentCatalogue>();
                AdjustmentCatalogue adCatObj;

                Catlist = (List<Catalogue>)ViewState["Vcatalist"];
                Control chkselect = null;
                string rstatus = "";
                string textBoxText;
                textBoxText = "";
                bool chkflag = false;
                bool rdflag = false;


            //Start
             for (int iRow = 0; iRow < gvCatalist.Rows.Count; iRow++)
              {
                    rdflag = false;
                    rstatus = "";
                    chkselect = gvCatalist.Rows[iRow].Cells[5].FindControl("myCheckBox");
               
                  if (chkselect != null)
                 {
                    if (((CheckBox)chkselect).Checked)
                    {
                        //string selCatlid = (String)gvCatalist.DataKeys[iRow].Value;
                        //Catalogue myCatObj = Catlist.Find(delegate(Catalogue cp) { return cp.ItemId == selCatlid; });
                        adCatObj = new AdjustmentCatalogue();

                        chkflag = true;

                        RadioButton rd1 = (RadioButton)gvCatalist.Rows[iRow].FindControl("rbDamage");
                        RadioButton rd2 = (RadioButton)gvCatalist.Rows[iRow].FindControl("rbGift");
                        textBoxText = ((TextBox)gvCatalist.Rows[iRow].FindControl("tbQty")).Text;


                        //Data insert into object
                        adCatObj.ItemId = gvCatalist.Rows[iRow].Cells[1].Text;
                        adCatObj.ItemName = gvCatalist.Rows[iRow].Cells[2].Text;

                        if (textBoxText != "")
                        {
                            adCatObj.AdjQty = Convert.ToInt32(textBoxText);
                        }
                        //adCatObj.Reason = gvCatalist.Rows[iRow].Cells[4].Text;



                        //int adjQty = Convert.ToInt32(textBoxText);
                        if (rd1.Checked == true)
                        {
                            rdflag = true;
                            rstatus = "Damage";
                            adCatObj.Reason = rstatus;
                        }
                        else if (rd2.Checked == true)
                        {
                            rdflag = true;
                            rstatus = "Gift";
                            adCatObj.Reason = rstatus;
                        }


                        if (rdflag == false)
                        {
                            lblmsg.Visible = true;
                            lblmsg.Text = "Please choose Adjustment Reason for every item.";
                        }

                        if (textBoxText == "")
                        {
                            lblmsg.Visible = true;
                            lblmsg.Text = "Please key in Adjustment Quantity.";

                        }
                        if (rdflag == true && textBoxText != "")
                        {
                            AdjCatlist.Add(adCatObj);

                        }
                    }
                }
            }
                   
                    //no Gvitem is checked.
                    if (chkflag == false)
                    {
                        lblmsg.Visible = true;
                        lblmsg.Text = "Please add desired item before Submission.";
                    }
                    //Add AdjCatlist to ViewState
                    ViewState["Padjlist"] = AdjCatlist;
           
                    if (chkflag == true) btnSubmit.Visible = true;
           

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
                List<Catalogue> Catlist = new List<Catalogue>();
                List<AdjustmentCatalogue> AdjCatlist = new List<AdjustmentCatalogue>();

                Catlist = (List<Catalogue>)ViewState["Vcatalist"];
                AdjCatlist = (List<AdjustmentCatalogue>)ViewState["Padjlist"];
            
                //To get Adjustment Voucher
                string SelCat = ViewState["selCat"].ToString();
                string submit_date = lblTodayDate.Text;
                int myresult = MakeStationeryAdjController.getMakeStationeryAdjController().Process(AdjCatlist, Catlist, SelCat, submit_date);
                lblmsg.Visible = true;
                lblmsg.Text = "Adjusted Items are successfully submitted for approval";

        }

        protected void myCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            
        }

      
    }

  
}